package org.xl.main.download;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class FixedFileDownloader extends JFrame {
    private JButton downloadButton;
    private JLabel infoLabel;

    private  String filePath;
    public FixedFileDownloader(String path) {
        this.filePath = path;
        setTitle("文件下载器");
        setSize(500, 150);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // 初始化组件
        downloadButton = new JButton("下载文件");
        infoLabel = new JLabel("点击按钮选择保存位置");
        infoLabel.setHorizontalAlignment(SwingConstants.CENTER);

        // 布局
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        
        panel.add(infoLabel, BorderLayout.CENTER);
        panel.add(downloadButton, BorderLayout.SOUTH);

        add(panel);

        // 事件监听
        downloadButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                File sourceFile = new File(filePath);
                
                if (!sourceFile.exists()) {
                    JOptionPane.showMessageDialog(null, 
                            "源文件不存在: " + filePath,
                            "错误", 
                            JOptionPane.ERROR_MESSAGE);
                    return;
                }

                JFileChooser fileChooser = new JFileChooser();
                fileChooser.setDialogTitle("选择保存位置");
                fileChooser.setSelectedFile(new File(sourceFile.getName()));
                
                int userSelection = fileChooser.showSaveDialog(null);
                
                if (userSelection == JFileChooser.APPROVE_OPTION) {
                    File saveLocation = fileChooser.getSelectedFile();
                    try {
                        // 复制文件到指定位置
                        Files.copy(sourceFile.toPath(), saveLocation.toPath());
                        JOptionPane.showMessageDialog(null, 
                            "文件下载成功!\n保存位置: " + saveLocation.getAbsolutePath(), 
                            "成功", 
                            JOptionPane.INFORMATION_MESSAGE);
                    } catch (IOException ex) {
                        JOptionPane.showMessageDialog(null, 
                            "下载失败: " + ex.getMessage(), 
                            "错误", 
                            JOptionPane.ERROR_MESSAGE);
                        ex.printStackTrace();
                    }
                }
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                String path = FixedFileDownloader.class.getClassLoader().getResource("file/export.zip").getPath();
                if (!File.separator.equals(":")) {
                    path = path.substring(1);
                }
                new FixedFileDownloader(path).setVisible(true);
            }
        });
    }
}